import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int count=Integer.valueOf(sc.nextLine());
        String s[]=new String[count];
        for (int i=0;i<count;i++){
            s[i]=sc.nextLine();
        }
        String temp;
        SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");
        while (true){
            String tem=sc.nextLine();
            if (tem.equals("sort1")){
                Date date[]=new Date[count];
                for (int j=0;j<count;j++){
                    for (int i=0;i<count-j-1;i++){
                        String bir1=s[i].substring(6,14);
                        String bir2=s[i+1].substring(6,14);
                        Date d1;
                        Date d2;
                        try {
                            d1 = df.parse(bir1);
                            d2 =df.parse(bir2);
                            if (d1.getTime()>d2.getTime()){
                                temp=s[i];
                                s[i]=s[i+1];
                                s[i+1]=temp;
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
                for (int i=0;i<count;i++){
                    String bir=s[i];
                    System.out.println(bir.substring(6,10)+"-"+bir.substring(10,12)+"-"+bir.substring(12,14));
                }
            }else if (tem.equals("sort2")){
                Date date[]=new Date[count];
                for (int j=0;j<count;j++){
                    for (int i=0;i<count-j-1;i++){
                        String bir1=s[i].substring(6,14);
                        String bir2=s[i+1].substring(6,14);
                        Date d1;
                        Date d2;
                        try {
                            d1 = df.parse(bir1);
                            d2 =df.parse(bir2);
                            if (d1.getTime()>d2.getTime()){
                                temp=s[i];
                                s[i]=s[i+1];
                                s[i+1]=temp;
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
                for (int i=0;i<count;i++){
                    System.out.println(s[i]);
                }

            }else {
                System.out.println("exit");
            }
        }
    }
}
